package com.itzol.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itzol.config.Result;
import com.itzol.entity.Recharge;
import com.itzol.entity.Reduce;
import com.itzol.entity.Users;
import com.itzol.service.RechargeService;
import com.itzol.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class RechargeController {
    @Autowired
    private UsersService usersService;
    @Autowired
    private RechargeService service;
    @PostMapping("/recharge")
    public Result recharge(@RequestBody Reduce reduce){
        QueryWrapper<Users> wrapper = new QueryWrapper<>();
        wrapper.eq("Phone",reduce.getPhone());
        Users users = usersService.getOne(wrapper);
        users.setMoney(users.getMoney()+reduce.getMoney());
        usersService.saveOrUpdate(users);
        Recharge recharge = new Recharge();
        recharge.setMoney(reduce.getMoney());
        recharge.setRecharger(users.getId());
        service.save(recharge);
        return Result.succ("加款成功");
    }
}
